package com.wu.getmedia;

import java.util.ArrayList;
import java.util.TreeSet;

/**
 * 数据流中的中位数
 * @author lynn
 * @date 2020/8/9 - 18:27
 */
public class Solution {
    public TreeSet<Integer> tree = new TreeSet<>();

    public void Insert(Integer num) {
        tree.add(num);
    }

    public Double GetMedian() {
        ArrayList<Integer> list = new ArrayList<>();
        list.addAll(tree);
        int index = list.size();
        if(index%2==1){
            return (double)list.get(index/2);
        }
        return ((double)(list.get(index/2-1))+(double)list.get(index/2))/2;
    }
}